import java.util.Date;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.util.CommandArgumentParser;
import com.pi4j.util.Console;


public class csb {

	public static void main(String[] args) throws InterruptedException {
		final Console console = new Console();

        // print program title/header
        console.title("<-- The Pi4J Project -->", "GPIO Input Example");

        // allow for user to exit program using CTRL-C
        console.promptForExit();
        
		 final GpioController gpio = GpioFactory.getInstance();

	        // by default we will use gpio pin #01; however, if an argument
	        // has been provided, then lookup the pin by address
	        Pin pin8 = CommandArgumentParser.getPin(
	                RaspiPin.class,    // pin provider class to obtain pin instance from
	                RaspiPin.GPIO_08,  // default pin if no pin argument found
	                args);             // argument array to search in

	        // by default we will use gpio pin PULL-UP; however, if an argument
	        // has been provided, then use the specified pull resistance
	        PinPullResistance pull = CommandArgumentParser.getPinPullResistance(
	                PinPullResistance.PULL_UP,  // default pin pull resistance if no pull argument found
	                args);                      // argument array to search in
	        Pin pin9 = CommandArgumentParser.getPin(
	                RaspiPin.class,    // pin provider class to obtain pin instance from
	                RaspiPin.GPIO_09,  // default pin if no pin argument found
	                args); 
	        
	        // provision gpio pin as an input pin
	        final GpioPinDigitalInput input = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05,pull);
	        final GpioPinDigitalOutput output = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "out", PinState.LOW);
				
	        while(!console.exiting()){
	        	check(input, output);
	        	Thread.sleep(1000);
	        	System.out.println("-----------------------------------------");
	        }
	        gpio.shutdown();
//			if (input.getState().isHigh()) {
//				end= new Date();
//				System.out.println("end:"+end.getTime());
//			}
//	        System.out.println((end.getTime()-st.getTime())*340/2);
//	        System.out.println("finish");

	}
	
	public static void check(GpioPinDigitalInput input,GpioPinDigitalOutput output) throws InterruptedException{
		
		output.setState(PinState.HIGH);
        
        Thread.sleep((long) 0.0015);
        output.setState(PinState.LOW);
        
        Date st= null;
        Date end = null;
		
		while (input.getState().isLow()) {
			
		}
		st= new Date();
		
		while (input.getState().isHigh()) {
			
		}
        
        end= new Date();
		
		System.out.println("dist:"+((end.getTime()-st.getTime())*0.34/2));
		
	}
}
